#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;


//method 1
class Solution2 {
    public:
        void sortColors(int A[], int n) {
            vector<int> count(3, 0);
            for (int i = 0; i < n; i++)
                count[A[i]]++;
            int index = 0;
            while(count[0] > 0) {A[index++] = 0; count[0]--;}
            while(count[1] > 0) {A[index++] = 1; count[1]--;}
            while(count[2] > 0) {A[index++] = 2; count[2]--;}
            return;
        }
};

void swap(int A[], int i, int j)
{
    int tmp = A[i];
    A[i] = A[j];
    A[j] = tmp;
}

//method 2 
class Solution {
    public:
        void sortColors(int A[], int n) {
            int i = 0, j = 0, k = n-1;
            while(j < k; ) {
                if (A[j] == 0) {
                    swap(A, i, j);
                    j++; 
                    i++;
                }
                else if (A[j] == 1) {
                    j++;
                } else {
                    swap(A, j, k);
                    k--;
                }
            }
            return;
        }
};

int main(int argc, char **argv)
{
    Solution mySolution;

    int input[] = {1, 2, 0, 2, 1}; 
    cout << "-----------------Test 1--------------------" << endl;
    mySolution.sortColors(input, sizeof(input)/sizeof(int));
    for (int i = 0; i < sizeof(input)/sizeof(int); i++)
        cout << input[i] << " ";

    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
